﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class kjxy_list : System.Web.UI.Page
{
    protected string parent_name = string.Empty;
    protected string fourm_name = string.Empty;
    protected List<Forums> fs;

    private static int all_pages = 0;
    private static int page = 1;
    private int size = 14;
    protected void Page_Load(object sender, EventArgs e)
    { 
        //if (!IsPostBack)
        //{
            TreasurerDataContext db = new TreasurerDataContext();
            //获取fourm_id
            string fourm_id = Request.QueryString["fourm_id"];
            //
            //获取同级栏目
            //
            var p = from aP in db.Forums where aP.ID.Equals(fourm_id) select aP;
            fourm_name = p.First().Name;
            if (p.First().ParentID == null)
            {
                parent_name = p.First().Name;
                //var t = from aT in db.Topic where aT.ID.Equals(fourm_id) select aT;
                fs = p.ToList<Forums>();
                //Repeater1.DataSource = p;
                //Repeater1.DataBind();
            }
            else
            {
                parent_name = (from aF in db.Forums where aF.ID.Equals(p.First().ParentID) select aF).First().Name;
                var t = from aT in db.Forums where aT.ParentID.Equals(p.First().ParentID) select aT;
                fs = t.ToList<Forums>();
                //Repeater1.DataSource = t;
                //Repeater1.DataBind();
            }

            //
            //获取数据
            //
            var ws = (from aA in db.Articles where aA.ForumID.Equals(fourm_id) orderby aA.Published descending select aA).Take(10);
            //Repeater2.DataSource = a;
            //Repeater2.DataBind();
            //总页数
            if (ws.Count() % size == 0)
            {
                all_pages = ws.Count() / size;
            }
            else
            {
                all_pages = ws.Count() / size + 1;
            }
            Paging(fourm_id, page);

        //}
    }
    protected void Paging(string id, int page)
    {
        TreasurerDataContext db = new TreasurerDataContext();
        var ns = from aN in db.Articles where aN.ForumID.Equals(id) orderby aN.Published descending select aN;
        if (ns.Count() == 0)
        {
            Repeater2.DataSource = ns;
            Repeater2.DataBind();
            Label1.Text = page + "/" + all_pages;
        }
        else
        {
            int coun=(page - 1) * size;
            List<Articles> ass = ns.Skip(coun).Take(size).ToList<Articles>();
            Repeater2.DataSource = ass;
            Repeater2.DataBind();
            Label1.Text = page + "/" + all_pages;
        }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        page = 1;
        string fourm_id = Request.QueryString["fourm_id"];
        Paging(fourm_id, page);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        page = all_pages;
        string fourm_id = Request.QueryString["fourm_id"];
        Paging(fourm_id, page);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (page > 1)
        {
            page--;
        }
        else
        {
            page = 1;
        }
        string fourm_id = Request.QueryString["fourm_id"];
        Paging(fourm_id, page);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (page < all_pages)
        {
            page++;
        }
        else
        {
            page = all_pages;
        }
        string fourm_id = Request.QueryString["fourm_id"];
        Paging(fourm_id, page);
    }
}